﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NextCMS.Models;
using System.Xml.Linq;

namespace NextCMS.DataAccess.XMLDataAccess
{
    internal class PageModules : IPageModule
    { 
        private string FilePath = HttpContext.Current.Server.MapPath("~") + "/App_Data/PageModules.xml";
        XDocument doc = null;
        public PageModules()
        {
            doc = XDocument.Load(FilePath);  //load the xml file.
        }
        public PageModuleModel GetById(int id)
        {
            
            var q = from d in doc.Descendants("PageModule")
                    where (int)d.Element("Id") == id
                    select new PageModuleModel
                    {
                        Id = (int)d.Element("Id"),
                        PageId = (int)d.Element("PageId"),
                        SectionId = new Guid(d.Element("SectionId").Value),
                        LanguageId = (int)d.Element("LanguageId"),
                        ModuleId = (int)d.Element("ModuleId"),
                        SubModuleId = (int)d.Element("SubModuleId"),
                        DisplayOrder = (int)d.Element("DisplayOrder"),
                        RefId = (int)d.Element("RefId")
                    };
            return q.FirstOrDefault();
        }
        public IEnumerable<PageModuleModel> GetById(int pageId, Guid sectionId,int LanguageId)
        {
            
            var q = from d in doc.Descendants("PageModule")
                    where (int)d.Element("PageId") == pageId && new Guid(d.Element("SectionId").Value) == sectionId
                    && (int)d.Element("LanguageId") == LanguageId
                    select new PageModuleModel
                    {
                        Id = (int)d.Element("Id"),
                        PageId = (int)d.Element("PageId"),
                        SectionId = new Guid(d.Element("SectionId").Value),
                        LanguageId = (int)d.Element("LanguageId"),
                        ModuleId = (int)d.Element("ModuleId"),
                        SubModuleId = (int)d.Element("SubModuleId"),
                        DisplayOrder = (int)d.Element("DisplayOrder"),
                        RefId = (int)d.Element("RefId")
                    };
            return q.AsEnumerable();
        }
        public IEnumerable<PageModuleModel> GetByRefId(int refId, int ModuleId, int SubModuleId, int Id)
        {
            
            var q = from d in doc.Descendants("PageModule")
                    where (int)d.Element("RefId") == refId && (int)d.Element("ModuleId") == ModuleId && (int)d.Element("SubModuleId") == SubModuleId
                    && (int)d.Element("Id") != Id
                    select new PageModuleModel
                    {
                        Id = (int)d.Element("Id"),
                        PageId = (int)d.Element("PageId"),
                        SectionId = new Guid(d.Element("SectionId").Value),
                        LanguageId = (int)d.Element("LanguageId"),
                        ModuleId = (int)d.Element("ModuleId"),
                        SubModuleId = (int)d.Element("SubModuleId"),
                        DisplayOrder = (int)d.Element("DisplayOrder"),
                        RefId = (int)d.Element("RefId")
                    };
            return q.AsEnumerable();
        }
        public IEnumerable<PageModuleModel> GetByPageId(int pageId)
        {
            
            var q = from d in doc.Descendants("PageModule")
                    where (int)d.Element("PageId") == pageId
                    select new PageModuleModel
                    {
                        Id = (int)d.Element("Id"),
                        PageId = (int)d.Element("PageId"),
                        SectionId = new Guid(d.Element("SectionId").Value),
                        LanguageId = (int)d.Element("LanguageId"),
                        ModuleId = (int)d.Element("ModuleId"),
                        SubModuleId = (int)d.Element("SubModuleId"),
                        DisplayOrder = (int)d.Element("DisplayOrder"),
                        RefId = (int)d.Element("RefId")
                    };
            return q.AsEnumerable();
        }
        public IEnumerable<PageModuleModel> GetById(Guid sectionId)
        {
           
            var q = from d in doc.Descendants("PageModule")
                    where new Guid(d.Element("SectionId").Value) == sectionId
                    select new PageModuleModel
                    {
                        Id = (int)d.Element("Id"),
                        PageId = (int)d.Element("PageId"),
                        SectionId = new Guid(d.Element("SectionId").Value),
                        LanguageId = (int)d.Element("LanguageId"),
                        ModuleId = (int)d.Element("ModuleId"),
                        SubModuleId = (int)d.Element("SubModuleId"),
                        DisplayOrder = (int)d.Element("DisplayOrder"),
                        RefId = (int)d.Element("RefId")
                    };
            return q.AsEnumerable();
        }
        public IEnumerable<PageModuleModel> GetAll()
        {
            
            var q = from d in doc.Descendants("PageModule")
                    select new PageModuleModel
                    {
                        Id = (int)d.Element("Id"),
                        PageId = (int)d.Element("PageId"),
                        SectionId = new Guid(d.Element("SectionId").Value),
                        LanguageId = (int)d.Element("LanguageId"),
                        ModuleId = (int)d.Element("ModuleId"),
                        SubModuleId = (int)d.Element("SubModuleId"),
                        DisplayOrder = (int)d.Element("DisplayOrder"),
                        RefId = (int)d.Element("RefId")
                    };
            return q;
        }
        public void Save(PageModuleModel page)
        {
            
            var q = from d in doc.Descendants("PageModule")
                    select new PageModuleModel
                    {
                        Id = (int)d.Element("Id"),

                    };

            if (q.Count() > 0)
                page.Id = q.Max(s => s.Id) + 1;
            else
                page.Id = 1;

            var oMember = new XElement("PageModule",
             new XElement("Id", page.Id),
             new XElement("PageId", page.PageId),
             new XElement("SectionId", page.SectionId),
             new XElement("LanguageId", page.LanguageId),
             new XElement("ModuleId", page.ModuleId),
             new XElement("SubModuleId", page.SubModuleId),
             new XElement("DisplayOrder", page.DisplayOrder),
             new XElement("RefId", page.RefId)
             );

            doc.Root.Add(oMember);
            doc.Save(FilePath);
        }
        // Delete Record
        public void Delete(int id)
        {
            PageModuleModel pmm = GetById(id);
            if (pmm.RefId > 0)
            {
                Modules mc = new Modules();
                ModuleModel mm = mc.GetById(pmm.ModuleId);
                if (mm.Code == "Content")
                {
                    PageContents pcc = new PageContents();
                    PageModules pmc = new PageModules();
                    if (pmc.GetByRefId(pmm.RefId, pmm.ModuleId, pmm.SubModuleId, pmm.Id).Count() == 0)
                        pcc.Delete(pmm.RefId);
                }
                else if (mm.Code == "Forms")
                {
                    PageForms pfc = new PageForms();
                    pfc.Delete(pmm.RefId);
                }
            }
            doc.Root.Elements("PageModule").Where(i => (int)i.Element("Id") == id).Remove();
            doc.Save(FilePath);
        }

        // Edit Record
        public void Edit(PageModuleModel page)
        {
            XElement node = doc.Root.Elements("PageModule").Where(i => (int)i.Element("Id") == page.Id).FirstOrDefault();
            node.SetElementValue("DisplayOrder", page.DisplayOrder);
            node.SetElementValue("RefId", page.RefId);
            doc.Save(FilePath);
        }

        public void Dispose()
        {
            doc = null;
        }
    }
}
